package com.liy.framework.common.redis.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Random;

public class RedisKeyUtils {

    public static final String TOKEN = "TK:";
    public static final String PERMISSION_MENU = "PER:MENU:";
    public static final String PERMISSION_ROLE = "PER:ROLE:";
    public static final String PERMISSION_ROLE_WEB = "PER:ROLEW:";
    public static final String SHARE_PERMISSION = "PER:COMM";
    public static final String SHARE_PERMISSION_WEB = "PER:COMMW";
    private static Random random = new Random();

    public RedisKeyUtils() {
    }

    public static String getTokenKey(String userId) {
        int i = random.nextInt(10000);
        if (i < 1000) {
            i += 1000;
        }

        return "TK:" + userId + ":" + i;
    }

    public static String getTokenKeyPrefix(String userId) {
        return "TK:" + userId;
    }

    public static String getRoleMenuKey(String roleId) {
        return "PER:MENU:" + roleId;
    }

    public static String getRolePermissionKey(String roleId) {
        return "PER:ROLE:" + roleId;
    }

    public static String getRolePermissionWebKey(String roleId) {
        return "PER:ROLEW:" + roleId;
    }

    public static String tokenId2base64(String tokenId) throws UnsupportedEncodingException {
        return Base64.getEncoder().encodeToString(tokenId.getBytes("UTF-8"));
    }

    public static String base642tokenId(String tokenId) throws UnsupportedEncodingException {
        return tokenId != null && !"".equals(tokenId.trim()) ? new String(Base64.getDecoder().decode(tokenId), "UTF-8") : null;
    }
}
